﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QConnectSDK;
using QConnectSDK.Models;
using System.IO;

namespace OpenConnect.WebSample.Account
{
    public partial class QQCallback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["code"] != null)
            {
                QOpenClient qzone = null;
                User currentUser = null;

                var verifier = Request.Params["code"];
                string state = Session["requeststate"].ToString();
                qzone = new QOpenClient(verifier, state);
                currentUser = qzone.GetCurrentUser();
                if (null != currentUser)
                {
                    this.result.Text = "成功登陆";
                    this.Nickname.Text = currentUser.Nickname;
                    this.Figureurl.ImageUrl = currentUser.Figureurl;
                }
                Session["QzoneOauth"] = qzone;

                //qzone.AddFeeds("张善友的QQ空间", "http://33235911.qzone.qq.com/");
                CheckPageResult bResult = qzone.CheckPagefans("42731209");
                this.lblResult.Text = bResult.Isfans.ToString();

                //var result = qzone.DelWeibo(44678063300294);
                //this.lblResult.Text = lblResult.Text +"　" + result.Data.Id;
                //string fileName = Server.MapPath("~/images") +"/Connect_logo_4.png";
                //var localFile = new FileInfo(fileName);
                //byte[] content = GetFileContentFromFS(localFile);
                //var result = qzone.AddWeiboPicture("通过QQ互联OAuth2.0API#分享图片#", "Connect_logo_4.png", content);
                //var result = qzone.GetWeiboUserInfo();
                //this.lblResult.Text = result.Data.Nick;

                //var content = qzone.GetRepostlist("2", "2755079366790", "0", "0", "100", "0");
                //var content = qzone.GetRepostlist("2", "97637003479598", "0", "0", "100", "0");
 
                //this.lblResult.Text = content.Data.Totalnum.ToString();
                //var other = qzone.GetIdolList(2,0);
                //this.lblResult.Text = other.Data.Hasnext.ToString();

                var result = qzone.GetTenpayAddr();
            }
        }
             /// <summary>
        /// Retrieve the content of a file in the local file system
        /// </summary>
        /// <param name="localFile">The local file to upload</param>
        /// <returns>True on success</returns>
        public byte[] GetFileContentFromFS(FileInfo localFile)
        {
            //Get the file stream
            byte[] bytes = null;
            FileStream fs = new FileStream(localFile.FullName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = localFile.Length;
            bytes = br.ReadBytes((int)numBytes);
            fs.Close();
            return bytes;
        }
    }
}